import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/LoginView.vue")
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('@/views/UserView.vue')
  },
  {
    path: '/store',
    name: 'store',
    component: () => import('@/views/StoreView.vue')
  },
  {
    path: '/coupon',
    name: 'coupon',
    component: () => import('@/views/CouponView.vue')
  },
  {
    path: '/type',
    name: 'type',
    component: () => import("@/views/TypeView.vue")
  }
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem("token")

  //如果进入的不是登陆页面，并且还没登陆过
  if(to.name != 'login' && token == null) next({name: 'login'})

  else{
    next()
  }
})

export default router
